package com.cloud.bc.controller.pc;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.controller.BaseController;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.*;
import com.cloud.bc.model.enums.PhotoEnum;
import com.cloud.bc.service.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

//PC单身中心资料
@Controller
@AllArgsConstructor
@RequestMapping("center/single")
public class PcCenterSingleController extends BaseController {
    private final BcUsersService bcUsersService;
    private final BcSinglesService bcSinglesService;
    private final BcSingleMatesService bcSingleMatesService;
    private final BcUserPhotosService bcUserPhotosService;
    private final BcSingleContactsService bcSingleContactsService;
    private final BcCategorysService bcCategorysService;
    private final BcCategoryDistrictsService bcCategoryDistrictsService;

    /**
     * 单身资料
     */
    @GetMapping("info")
    public ModelAndView info(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "单身资料-用户中心", "pc/center/single/info");
        view.addObject("bodyCss", "page_manage page_manage_singleinfo");

        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcSingles bcSingles = bcSinglesService.selectOne(
                new BcSingles().setUserId(userInfo.getId()));
        view.addObject("singles", bcSingles);
        //身高
        view.addObject("baseHeight", getHeight());
        //体重
        view.addObject("baseWeight", getWeight());
        //收入
        view.addObject("baseIncome", bcCategorysService.getCategoryList("BC_income"));
        //学历
        view.addObject("baseEducation", bcCategorysService.getCategoryList("BC_education"));
        //婚姻状态
        view.addObject("baseMarriage", bcCategorysService.getCategoryList("BC_marriage"));
        //孩子
        view.addObject("baseChildren", bcCategorysService.getCategoryList("BC_children"));

        //结婚计划
        view.addObject("baseMarry", bcCategorysService.getCategoryList("BC_marry"));
        //民族
        view.addObject("baseNationality", bcCategorysService.getCategoryList("BC_nationality"));
        //性格
        view.addObject("baseDisposition", bcCategorysService.getCategoryList("BC_disposition"));
        //购房情况
        view.addObject("baseHouse", bcCategorysService.getCategoryList("BC_house"));
        //购车情况
        view.addObject("baseCar", bcCategorysService.getCategoryList("BC_car"));
        //从事行业
        view.addObject("baseIndustry", bcCategorysService.getCategoryList("BC_industry"));
        //从事职业
        view.addObject("baseProfession", bcCategorysService.getCategoryList("BC_profession"));
        //家庭背景
        view.addObject("baseFamilybackg", bcCategorysService.getCategoryList("BC_family_backg"));
        //喝酒
        view.addObject("baseDrink", bcCategorysService.getCategoryList("BC_drink"));
        //抽烟
        view.addObject("baseSmoking", bcCategorysService.getCategoryList("BC_smoking"));
        //健身习惯
        view.addObject("baseWorkout", bcCategorysService.getCategoryList("BC_work_out"));
        //作息习惯
        view.addObject("baseWorkrest", bcCategorysService.getCategoryList("BC_work_rest"));
        //宠物
        view.addObject("basePet", bcCategorysService.getCategoryList("BC_pet"));
        //宗教信仰
        view.addObject("baseReligious", bcCategorysService.getCategoryList("BC_religious"));
        //兴趣爱好
        view.addObject("baseHobby", bcCategorysService.getCategoryList("BC_hobby"));

        List<BcCategoryDistricts> allCategoryDistrictsCache = bcCategoryDistrictsService.getAllCategoryDistrictsCache();

        List<BcCategoryDistricts> listProvince =
                allCategoryDistrictsCache.stream().filter(t -> t.getParentId().equals(0)).collect(Collectors.toList());
        view.addObject("listProvince", listProvince);
//所在区域
        viewCategoryDistricts(bcSingles.getDistrict(), allCategoryDistrictsCache, "distrcit", view);
//籍贯
        viewCategoryDistricts(bcSingles.getNativePlace(), allCategoryDistrictsCache, "nativeplace", view);
        return view;
    }

    /**
     * 择偶标准
     */
    @GetMapping("mate")
    public ModelAndView mate(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "单身资料-用户中心", "pc/center/single/mate");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcSingleMates bcSingleMates = bcSingleMatesService.selectOne(
                new BcSingleMates().setUserId(userInfo.getId()));
        view.addObject("mate", bcSingleMates);
        view.addObject("bodyCss", "page_manage page_manage_singleinfo");
        //身高
        view.addObject("baseHeight", getHeight());
        //体重
        view.addObject("baseWeight", getWeight());
        //年龄
        view.addObject("baseAge",getAge());
//收入
        view.addObject("baseIncome", bcCategorysService.getCategoryList("BC_income"));
//学历
        view.addObject("baseEducation", bcCategorysService.getCategoryList("BC_education"));
        List<BcCategoryDistricts> allCategoryDistrictsCache = bcCategoryDistrictsService.getAllCategoryDistrictsCache();

        List<BcCategoryDistricts> listProvince =
                allCategoryDistrictsCache.stream().filter(t -> t.getParentId().equals(0)).collect(Collectors.toList());
        view.addObject("listProvince", listProvince);
//所在区域
        viewCategoryDistricts(bcSingleMates.getMateDistrict(), allCategoryDistrictsCache, "distrcit", view);
//籍贯
        viewCategoryDistricts(bcSingleMates.getMateNativePlace(), allCategoryDistrictsCache, "nativeplace", view);
//民族
        view.addObject("baseNationality", bcCategorysService.getCategoryList("BC_nationality"));
//婚姻状态
        view.addObject("baseMarriage", bcCategorysService.getCategoryList("BC_marriage"));
//孩子
        view.addObject("baseChildren", bcCategorysService.getCategoryList("BC_children"));
//购房情况
        view.addObject("baseHouse", bcCategorysService.getCategoryList("BC_house"));
//购车情况
        view.addObject("baseCar", bcCategorysService.getCategoryList("BC_car"));
//宗教信仰
        view.addObject("baseReligious", bcCategorysService.getCategoryList("BC_religious"));
//性格
        view.addObject("baseDisposition", bcCategorysService.getCategoryList("BC_disposition"));
//结婚计划
        view.addObject("baseMarry", bcCategorysService.getCategoryList("BC_marry"));
//家庭背景
        view.addObject("baseFamilybackg", bcCategorysService.getCategoryList("BC_family_backg"));
        return view;
    }

    /**
     * 头象
     */
    @GetMapping("photo")
    public ModelAndView photo(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "头象-用户中心", "pc/center/single/photo");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcSingles bcSingles = bcSinglesService.selectOne(
                new BcSingles().setUserId(userInfo.getId()));
        view.addObject("singles", bcSingles);
        view.addObject("bodyCss", "page_manage page_manage_singleinfo");
        return view;
    }

    /**
     * 个性展示
     */
    @GetMapping("show")
    public ModelAndView show(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "头象-用户中心", "pc/center/single/show");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcSingles bcSingles = bcSinglesService.selectOne(
                new BcSingles().setUserId(userInfo.getId()));
        view.addObject("singles", bcSingles);
        view.addObject("bodyCss", "page_manage page_manage_singleinfo");
        return view;
    }

    //区域
    private void viewCategoryDistricts(String district,
                                       List<BcCategoryDistricts> allCategoryDistrictsCache,
                                       String viewType,
                                       ModelAndView view) {
        //显示省市区
        if (StrUtil.isEmpty(district) || district.equals("..") || district.equals(".")) {
            return;
        }
        String[] districtArr = district.split("\\.");
        Integer provinceId = Convert.toInt(districtArr[0], 0);
        Integer cityId = Convert.toInt(districtArr[1], 0);
        Integer areaId = Convert.toInt(districtArr[2], 0);
        if (provinceId > 0) {

            List<BcCategoryDistricts> listCity =
                    allCategoryDistrictsCache.stream().filter(t -> t.getParentId().equals(provinceId)).collect(Collectors.toList());
            view.addObject(viewType + "ProvinceId", provinceId);
            view.addObject(viewType + "ListCity", listCity);
        }
        if (cityId > 0) {
            List<BcCategoryDistricts> listArea =
                    allCategoryDistrictsCache.stream().filter(t -> t.getParentId().equals(cityId)).collect(Collectors.toList());
            view.addObject(viewType + "CityId", cityId);
            view.addObject(viewType + "ListArea", listArea);
            view.addObject(viewType + "AreaId", areaId);
        }
    }

    private List<Integer> getHeight() {
        List<Integer> list = new ArrayList<>();
        for (int i = 145; i < 246; i++) {
            list.add(i);
        }
        return list;
    }

    private List<Integer> getWeight() {
        List<Integer> list = new ArrayList<>();
        for (int i = 40; i < 201; i++) {
            list.add(i);
        }
        return list;
    }

    private List<Integer> getAge() {
        List<Integer> list = new ArrayList<>();
        for (int i = 18; i < 81; i++) {
            list.add(i);
        }
        return list;
    }
}
